package com.api.user.controller;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.user.model.Task;
import com.api.user.service.TaskService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.api.core.annotation.PowerEnable;
import io.swagger.annotations.*;



/**
* Created by wanghuiwen on 2021/04/23.
*/
@PowerEnable(name = "任务",url = "/task")
@Api(value = "任务", tags = {"任务"})
@RestController
@RequestMapping("/task")
public class TaskController extends Ctrl{
    @Resource
    private TaskService taskService;

    @ApiOperation(value = "任务详细信息", tags = {"任务"}, notes = "任务详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",required=true, value = "任务id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/detail",name="任务详细信息")
    public Result detail(@RequestParam Integer id) {
        Task task = taskService.findById(id);
        return ResultGenerator.genSuccessResult(task);
    }

    @ApiOperation(value = "任务列表信息", tags = {"任务"}, notes = "任务列表信息")
    @PostMapping(value = "/user/list", name = "任务列表信息")
    public Result list(Authentication authentication) {
        return taskService.list(getAuthUser(authentication));
    }

    @ApiOperation(value = "任务列表信息", tags = {"任务"}, notes = "领取任务奖励")
    @PostMapping(value = "/receive", name = "任务列表信息")
    public Result receive(Long userTaskId ,Authentication authentication) {
        return taskService.receive(userTaskId,getAuthUser(authentication).getId());
    }

    @ApiOperation(value = "列表", tags = {"任务"}, notes = "列表")
    @PostMapping(value = "/list", name = "列表")
    public Result listAll( @RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        List<Task> tasks = taskService.findAll();
        PageInfo<Task> pageInfo = new PageInfo<>(tasks);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


}
